Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 484)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.36936 11.42567 11.48098 11.53531 11.58867 11.64106 11.69249 11.74297
##   [9] 11.79250 11.84110 11.88876 11.93551 11.98134 12.02626 12.07028 12.11341
##  [17] 12.15565 12.19701 12.23751 12.27714 12.31590 12.35378 12.39075 12.42681
##  [25] 12.46194 12.49611 12.52932 12.56156 12.59280 12.62303 12.65224 12.68041
##  [33] 12.70752 12.73356 12.75872 12.78316 12.80682 12.82967 12.85166 12.87273
##  [41] 12.89285 12.91196 12.93001 12.94696 12.96277 12.97737 12.99074 13.00281
##  [49] 13.01393 13.02443 13.03424 13.04328 13.05149 13.05878 13.06508 13.07033
##  [57] 13.07445 13.07736 13.07899 13.07927 13.07813 13.07549 13.07014 13.06119
##  [65] 13.04903 13.03408 13.01675 12.99743 12.97654 12.95448 12.93167 12.90849
##  [73] 12.88537 12.86270 12.84090 12.82037 12.80152 12.78475 12.76703 12.74547
##  [81] 12.72070 12.69336 12.66408 12.63351 12.60227 12.57100 12.54034 12.51092
##  [89] 12.48337 12.45833 12.43644 12.41833 12.40153 12.38336 12.36413 12.34416
##  [97] 12.32375 12.30323 12.28291 12.26310 12.24412 12.22628 12.20989 12.19528
## [105] 12.18275 12.17261 12.16501 12.15968 12.15632 12.15463 12.15432 12.15509
## [113] 12.15665 12.15870 12.16094 12.16309 12.16484 12.16589 12.16596 12.16474
## [121] 12.16304 12.16184 12.16113 12.16089 12.16111 12.16177 12.16286 12.16435
## [129] 12.16624 12.16850 12.17112 12.17408 12.17737 12.18097 12.18487 12.18904
## [137] 12.19348 12.19816 12.20307 12.20814 12.21333 12.21867 12.22422 12.23002
## [145] 12.23612 12.24255 12.24936 12.25659 12.26430 12.27251 12.28129 12.29066
## [153] 12.30067 12.31138 12.32281 12.33663 12.35407 12.37451 12.39735 12.42200
## [161] 12.44783 12.47426 12.50068 12.52648 12.55105 12.57381 12.59413 12.61141
## [169] 12.62506 12.63811 12.65377 12.67170 12.69156 12.71299 12.73566 12.75921
## [177] 12.78332 12.80763 12.83179 12.85547 12.87832 12.89999 12.92015 12.93844
## [185] 12.95453 12.96807 12.97871 12.98611 12.98993 12.98981 12.98696 12.98282
## [193] 12.97743 12.97083 12.96306 12.95417 12.94420 12.93318 12.92118 12.90821
## [201] 12.89434 12.87960 12.86403 12.84767 12.82742 12.80075 12.76859 12.73186
## [209] 12.69147 12.64834 12.60340 12.55756 12.51176 12.46689 12.42390 12.38369
## [217] 12.34718 12.31531 12.28282 12.24456 12.20154 12.15477 12.10528 12.05408
## [225] 12.00219 11.95063 11.90041 11.85255 11.80808 11.76800 11.73334 11.70511
## [233] 11.67960 11.65273 11.62494 11.59669 11.56844 11.54063 11.51371 11.48814
## [241] 11.46437 11.44285 11.42404 11.40837 11.39632 11.38785 11.38252 11.38007
## [249] 11.38026 11.38287 11.38764 11.39435 11.40276 11.41262 11.42370 11.43576
## [257] 11.44856 11.46187 11.47544 11.48905 11.50244 11.51538 11.52764 11.53897
## [265] 11.54914 11.56042 11.57493 11.59219 11.61172 11.63306 11.65573 11.67925
## [273] 11.70315 11.72696 11.75020 11.77240 11.79309 11.81178 11.82801 11.84352
## [281] 11.86025 11.87800 11.89659 11.91583 11.93553 11.95549 11.97554 11.99547
## [289] 12.01511 12.03426 12.05273 12.07033 12.08687 12.10254 12.11769 12.13238
## [297] 12.14667 12.16061 12.17427 12.18770 12.20097 12.21412 12.22723 12.24034
## [305] 12.25352 12.26683 12.28032 12.29388 12.30737 12.32077 12.33408 12.34730
## [313] 12.36040 12.37340 12.38629 12.39904 12.41167 12.42416 12.43651 12.44871
## [321] 12.46076 12.47264 12.48435 12.49664 12.51011 12.52453 12.53970 12.55538
## [329] 12.57137 12.58744 12.60337 12.61895 12.63395 12.64815 12.66134 12.67329
## [337] 12.68379 12.69317 12.70193 12.71011 12.71777 12.72494 12.73167 12.73800
## [345] 12.74397 12.74963 12.75502 12.76019 12.76518 12.77003 12.77479 12.77926
## [353] 12.78324 12.78677 12.78987 12.79257 12.79491 12.79692 12.79862 12.80005
## [361] 12.80125 12.80223 12.80304 12.80370 12.80424 12.80399 12.80236 12.79952
## [369] 12.79561 12.79081 12.78527 12.77916 12.77263 12.76585 12.75899 12.75219
## [377] 12.74563 12.73946 12.73385 12.72895 12.72493 12.72195 12.72017 12.71975
## [385] 12.72001 12.72016 12.72027 12.72037 12.72051 12.72074 12.72111 12.72165
## [393] 12.72242 12.72347 12.72483 12.72657 12.72871 12.73131 12.73528 12.74126
## [401] 12.74888 12.75779 12.76762 12.77801 12.78860 12.79902 12.80891 12.81792
## [409] 12.82566 12.83180 12.83595 12.83777 12.83863 12.84007 12.84197 12.84420
## [417] 12.84661 12.84910 12.85152 12.85375 12.85565 12.85711 12.85798 12.85814
## [425] 12.85746 12.85582 12.85286 12.84845 12.84280 12.83610 12.82855 12.82036
## [433] 12.81172 12.80284 12.79390 12.78512 12.77669 12.76880 12.76167 12.75548
## [441] 12.75044 12.74675 12.74356 12.73992 12.73591 12.73162 12.72712 12.72248
## [449] 12.71780 12.71313 12.70857 12.70420 12.70008 12.69630 12.69293 12.69005
## [457] 12.68738 12.68458 12.68169 12.67875 12.67580 12.67287 12.66999 12.66722
## [465] 12.66458 12.66211 12.65984 12.65782 12.65608 12.65465 12.65359 12.65291
## [473] 12.65257 12.65255 12.65282 12.65336 12.65414 12.65513 12.65630 12.65763
## [481] 12.65909 12.66066 12.66230 12.66400
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 484)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.88780 10.96217 11.03530 11.10719 11.17783 11.24720 11.31529 11.38209
##   [9] 11.44759 11.51178 11.57465 11.63618 11.69637 11.75519 11.81266 11.86874
##  [17] 11.92343 11.97672 12.02860 12.07905 12.12807 12.17567 12.22189 12.26674
##  [25] 12.31025 12.35246 12.39339 12.43306 12.47150 12.50875 12.54482 12.57974
##  [33] 12.61355 12.64627 12.67756 12.70714 12.73509 12.76150 12.78646 12.81003
##  [41] 12.83231 12.85339 12.87335 12.89226 12.91022 12.92731 12.94361 12.95921
##  [49] 12.97305 12.98419 12.99290 12.99946 13.00412 13.00717 13.00886 13.00948
##  [57] 13.00928 13.00854 13.00753 13.00652 13.00577 13.00556 13.00488 13.00260
##  [65] 12.99888 12.99382 12.98758 12.98027 12.97203 12.96299 12.95328 12.94303
##  [73] 12.93236 12.92142 12.91032 12.89921 12.88821 12.87745 12.86630 12.85409
##  [81] 12.84090 12.82682 12.81194 12.79635 12.78012 12.76335 12.74612 12.72852
##  [89] 12.71063 12.69254 12.67434 12.65611 12.63552 12.61067 12.58227 12.55107
##  [97] 12.51777 12.48310 12.44779 12.41255 12.37812 12.34521 12.31454 12.28685
## [105] 12.26285 12.24327 12.22488 12.20431 12.18205 12.15859 12.13443 12.11006
## [113] 12.08598 12.06267 12.04064 12.02037 12.00236 11.98711 11.97510 11.96683
## [121] 11.96107 11.95624 11.95236 11.94940 11.94736 11.94624 11.94604 11.94675
## [129] 11.94836 11.95087 11.95427 11.95856 11.96373 11.96979 11.97671 11.98451
## [137] 11.99316 12.00268 12.01305 12.02597 12.04281 12.06309 12.08634 12.11207
## [145] 12.13981 12.16906 12.19935 12.23021 12.26114 12.29167 12.32131 12.34960
## [153] 12.37603 12.40015 12.42146 12.44383 12.47093 12.50195 12.53609 12.57253
## [161] 12.61047 12.64912 12.68765 12.72528 12.76118 12.79457 12.82462 12.85054
## [169] 12.87152 12.89043 12.91051 12.93156 12.95335 12.97568 12.99831 13.02105
## [177] 13.04367 13.06595 13.08768 13.10864 13.12862 13.14740 13.16476 13.18049
## [185] 13.19437 13.20618 13.21571 13.22274 13.22706 13.22845 13.22793 13.22662
## [193] 13.22448 13.22146 13.21750 13.21254 13.20655 13.19946 13.19123 13.18180
## [201] 13.17113 13.15915 13.14582 13.13108 13.11294 13.08989 13.06257 13.03164
## [209] 12.99777 12.96161 12.92383 12.88508 12.84601 12.80730 12.76959 12.73355
## [217] 12.69983 12.66910 12.63718 12.60005 12.55856 12.51357 12.46594 12.41652
## [225] 12.36618 12.31577 12.26614 12.21816 12.17269 12.13058 12.09269 12.05988
## [233] 12.02767 11.99157 11.95241 11.91102 11.86823 11.82487 11.78178 11.73978
## [241] 11.69970 11.66237 11.62863 11.59931 11.57523 11.55483 11.53594 11.51855
## [249] 11.50262 11.48811 11.47500 11.46324 11.45282 11.44370 11.43584 11.42921
## [257] 11.42379 11.41953 11.41642 11.41440 11.41346 11.41356 11.41467 11.41676
## [265] 11.41979 11.42515 11.43394 11.44575 11.46013 11.47664 11.49486 11.51434
## [273] 11.53467 11.55539 11.57608 11.59629 11.61561 11.63359 11.64979 11.66653
## [281] 11.68609 11.70809 11.73211 11.75773 11.78455 11.81216 11.84015 11.86810
## [289] 11.89562 11.92228 11.94768 11.97141 11.99307 12.01483 12.03890 12.06488
## [297] 12.09237 12.12098 12.15030 12.17995 12.20951 12.23860 12.26681 12.29374
## [305] 12.31901 12.34220 12.36292 12.38205 12.40073 12.41897 12.43681 12.45425
## [313] 12.47131 12.48802 12.50439 12.52045 12.53620 12.55167 12.56687 12.58183
## [321] 12.59656 12.61109 12.62543 12.63942 12.65292 12.66597 12.67862 12.69091
## [329] 12.70286 12.71454 12.72597 12.73719 12.74826 12.75920 12.77006 12.78088
## [337] 12.79170 12.80206 12.81156 12.82029 12.82836 12.83587 12.84293 12.84963
## [345] 12.85609 12.86241 12.86868 12.87502 12.88153 12.88831 12.89546 12.90273
## [353] 12.90981 12.91670 12.92341 12.92995 12.93634 12.94258 12.94868 12.95465
## [361] 12.96051 12.96626 12.97191 12.97747 12.98296 12.98828 12.99336 12.99822
## [369] 13.00287 13.00735 13.01166 13.01584 13.01989 13.02384 13.02772 13.03153
## [377] 13.03530 13.03906 13.04281 13.04659 13.05041 13.05429 13.05825 13.06232
## [385] 13.06673 13.07166 13.07698 13.08259 13.08837 13.09422 13.10002 13.10566
## [393] 13.11103 13.11602 13.12051 13.12440 13.12757 13.12990 13.13230 13.13557
## [401] 13.13951 13.14391 13.14855 13.15321 13.15770 13.16180 13.16530 13.16798
## [409] 13.16964 13.17005 13.16902 13.16633 13.16222 13.15715 13.15122 13.14454
## [417] 13.13723 13.12938 13.12112 13.11254 13.10375 13.09487 13.08600 13.07726
## [425] 13.06874 13.06055 13.05161 13.04090 13.02870 13.01527 13.00088 12.98579
## [433] 12.97026 12.95456 12.93896 12.92373 12.90912 12.89540 12.88284 12.87170
## [441] 12.86224 12.85475 12.84814 12.84125 12.83414 12.82690 12.81961 12.81233
## [449] 12.80515 12.79815 12.79139 12.78497 12.77896 12.77342 12.76845 12.76412
## [457] 12.76013 12.75616 12.75224 12.74842 12.74472 12.74120 12.73788 12.73481
## [465] 12.73202 12.72955 12.72744 12.72573 12.72445 12.72364 12.72334 12.72354
## [473] 12.72423 12.72536 12.72692 12.72888 12.73122 12.73390 12.73692 12.74023
## [481] 12.74382 12.74765 12.75171 12.75597
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 484)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.87352 10.93192 10.98935 11.04581 11.10130 11.15582 11.20936 11.26193
##   [9] 11.31351 11.36412 11.41373 11.46237 11.51001 11.55666 11.60231 11.64697
##  [17] 11.69063 11.73329 11.77494 11.81559 11.85524 11.89392 11.93161 11.96833
##  [25] 12.00406 12.03881 12.07258 12.10537 12.13718 12.16801 12.19785 12.22671
##  [33] 12.25459 12.28149 12.30727 12.33183 12.35522 12.37747 12.39864 12.41875
##  [41] 12.43785 12.45599 12.47320 12.48953 12.50502 12.51971 12.53364 12.54686
##  [49] 12.55921 12.57054 12.58087 12.59023 12.59863 12.60612 12.61270 12.61841
##  [57] 12.62327 12.62730 12.63053 12.63299 12.63469 12.63567 12.63505 12.63209
##  [65] 12.62704 12.62013 12.61161 12.60171 12.59066 12.57871 12.56610 12.55307
##  [73] 12.53985 12.52668 12.51381 12.50146 12.48988 12.47931 12.46782 12.45360
##  [81] 12.43708 12.41871 12.39890 12.37810 12.35673 12.33523 12.31403 12.29356
##  [89] 12.27426 12.25655 12.24087 12.22764 12.21514 12.20146 12.18684 12.17149
##  [97] 12.15563 12.13948 12.12325 12.10718 12.09147 12.07635 12.06204 12.04875
## [105] 12.03671 12.02613 12.01681 12.00833 12.00059 11.99350 11.98695 11.98084
## [113] 11.97508 11.96957 11.96420 11.95888 11.95350 11.94798 11.94220 11.93608
## [121] 11.92945 11.92229 11.91472 11.90683 11.89875 11.89058 11.88243 11.87440
## [129] 11.86661 11.85917 11.85218 11.84575 11.83999 11.83502 11.83094 11.82785
## [137] 11.82587 11.82511 11.82568 11.82669 11.82729 11.82762 11.82785 11.82810
## [145] 11.82853 11.82928 11.83050 11.83234 11.83494 11.83844 11.84301 11.84877
## [153] 11.85589 11.86449 11.87474 11.88757 11.90352 11.92214 11.94295 11.96551
## [161] 11.98936 12.01404 12.03908 12.06404 12.08846 12.11187 12.13382 12.15384
## [169] 12.17149 12.19010 12.21298 12.23963 12.26957 12.30228 12.33729 12.37409
## [177] 12.41219 12.45110 12.49032 12.52935 12.56770 12.60488 12.64039 12.67373
## [185] 12.70441 12.73194 12.75582 12.77556 12.79066 12.80062 12.80762 12.81409
## [193] 12.81994 12.82508 12.82940 12.83282 12.83524 12.83657 12.83672 12.83558
## [201] 12.83307 12.82908 12.82354 12.81634 12.80465 12.78636 12.76239 12.73369
## [209] 12.70122 12.66591 12.62872 12.59058 12.55244 12.51524 12.47994 12.44747
## [217] 12.41878 12.39482 12.37098 12.34256 12.31033 12.27507 12.23755 12.19855
## [225] 12.15884 12.11920 12.08040 12.04323 12.00845 11.97684 11.94918 11.92624
## [233] 11.90542 11.88377 11.86155 11.83902 11.81643 11.79405 11.77212 11.75091
## [241] 11.73068 11.71167 11.69416 11.67838 11.66462 11.65267 11.64215 11.63298
## [249] 11.62510 11.61844 11.61294 11.60853 11.60515 11.60272 11.60117 11.60046
## [257] 11.60049 11.60122 11.60257 11.60448 11.60687 11.60969 11.61286 11.61633
## [265] 11.62001 11.62503 11.63232 11.64158 11.65248 11.66471 11.67796 11.69192
## [273] 11.70626 11.72068 11.73486 11.74849 11.76124 11.77282 11.78290 11.79232
## [281] 11.80208 11.81216 11.82251 11.83311 11.84391 11.85488 11.86598 11.87717
## [289] 11.88842 11.89970 11.91096 11.92217 11.93329 11.94536 11.95926 11.97469
## [297] 11.99136 12.00899 12.02728 12.04596 12.06472 12.08327 12.10134 12.11863
## [305] 12.13485 12.14971 12.16292 12.17573 12.18946 12.20394 12.21898 12.23443
## [313] 12.25012 12.26585 12.28148 12.29682 12.31170 12.32595 12.33939 12.35187
## [321] 12.36319 12.37320 12.38172 12.38874 12.39448 12.39911 12.40277 12.40561
## [329] 12.40780 12.40949 12.41083 12.41199 12.41310 12.41434 12.41586 12.41780
## [337] 12.42033 12.42289 12.42489 12.42638 12.42745 12.42818 12.42863 12.42889
## [345] 12.42902 12.42911 12.42923 12.42945 12.42984 12.43049 12.43147 12.43198
## [353] 12.43132 12.42967 12.42722 12.42415 12.42065 12.41690 12.41309 12.40941
## [361] 12.40604 12.40318 12.40100 12.39969 12.39943 12.39961 12.39951 12.39918
## [369] 12.39868 12.39806 12.39738 12.39669 12.39605 12.39551 12.39512 12.39495
## [377] 12.39504 12.39545 12.39624 12.39746 12.39916 12.40140 12.40423 12.40772
## [385] 12.41252 12.41910 12.42715 12.43639 12.44652 12.45726 12.46830 12.47937
## [393] 12.49018 12.50042 12.50981 12.51805 12.52487 12.52995 12.53471 12.54057
## [401] 12.54729 12.55467 12.56246 12.57045 12.57840 12.58609 12.59329 12.59978
## [409] 12.60532 12.60970 12.61268 12.61403 12.61406 12.61326 12.61174 12.60956
## [417] 12.60683 12.60363 12.60005 12.59618 12.59210 12.58791 12.58370 12.57954
## [425] 12.57554 12.57177 12.56737 12.56153 12.55447 12.54639 12.53750 12.52802
## [433] 12.51815 12.50810 12.49808 12.48829 12.47895 12.47026 12.46244 12.45569
## [441] 12.45022 12.44625 12.44290 12.43923 12.43531 12.43122 12.42703 12.42281
## [449] 12.41864 12.41459 12.41073 12.40714 12.40389 12.40105 12.39870 12.39692
## [457] 12.39543 12.39394 12.39249 12.39111 12.38981 12.38863 12.38760 12.38675
## [465] 12.38610 12.38569 12.38553 12.38566 12.38611 12.38691 12.38808 12.38963
## [473] 12.39154 12.39378 12.39634 12.39919 12.40233 12.40571 12.40934 12.41318
## [481] 12.41722 12.42143 12.42580 12.43031
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")